<style>
    .extendimg img {
        width: 60px;
    }

    .extendinfo {
        margin-left: 15px;
        font-size: 13px;
    }

    .extendinfo div {
        margin-top: 2px;
        color: #666;
    }

    .extendinfo .title {
        font-size: 14px;
        font-weight: 600;
    }

    .elist {
        padding-top: 20px;
        padding-bottom: 20px;
        border-bottom: 1px dashed #ccc;
    }
</style>
<div class="rightcontent rc-bg-grey">
    <div class="rc-pagepadding">
        <div class="layui-tab layui-tab-brief" lay-filter="tabfilter">

            <ul class="layui-tab-title">

                <li class="layui-this">超管列表</li>

                <li>添加超管</li>


            </ul>

            <div class="layui-tab-content">

                <div class="layui-tab-item layui-show">

                    <table class="layui-table">
                        <colgroup>
                            <col>
                            <col>
                            <col>
                            <col>
                        </colgroup>
                        <thead>
                            <tr>
                                <th>姓名</th>
                                <th>用户名</th>
                                <th>手机</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody id="admins">

                        </tbody>
                    </table>

                </div>

                <div class="layui-tab-item">


                    <div class="rc-pannel rc-searchbox">

                        <form class="layui-form " method="get">
                            <div class="rc-flex-vertical-center rc-searchbox">

                                <div class="layui-inline ">
                                    <label class="layui-form-label">用户名</label>
                                    <div class="layui-input-inline ">
                                        <input type="text" name="username" id="username" placeholder="" value="" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-inline ">
                                    <label class="layui-form-label">手机</label>
                                    <div class="layui-input-inline ">
                                        <input type="text" name="phone" id="phone" placeholder="" value="" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-form-label">
                                        <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="searchuser"><i class="iconfont">&#xeafe;</i> 搜索</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>


                    <div class=" rc-pannel rc-margin-top-default">
                        <div class="rc-pannel-body layui-form rc-pagepadding ">

                            <table class="layui-table">
                                <colgroup>
                                    <col>
                                    <col>
                                    <col>
                                    <col>
                                </colgroup>
                                <thead>
                                    <tr>
                                        <th>姓名</th>
                                        <th>用户名</th>
                                        <th>手机</th>
                                        <th>选择</th>
                                    </tr>
                                </thead>
                                <tbody id="users">

                                </tbody>
                            </table>
                        </div>

                        <div class="rc-pannel-footer">
                            <div class="pagebar" id="pagebar" style="text-align: right;">

                            </div>

                        </div>
                    </div>

                </div>
            </div>

        </div>






    </div>
</div>
<script type='text/javascript'>
    layui.use(function() {
        var form = layui.form;
        var element = layui.element;
        var laypage = layui.laypage;
        $('#searchuser').click(function() {
            seluser();
        });


        selAdmin();

        // tab 切换事件
        element.on('tab(tabfilter)', function(data) {

            if (data.index == 0) {
                selAdmin();
            }

        })


        function seluser() {
            $.ajax({
                url: '/admin/cnf/Site/systemAdmin?type=seluser', //请求路径
                type: "get", //请求方式
                data: {
                    username_phone: $('#username_phone').val()
                }, // 默认写法
                success: function(data) {
                    var users = '';
                    if (data.code == 0) {
                        if (data.data.users.length > 0) {
                            $.each(data.data.users, function(i, v) {
                                users += ` <tr>
                                        <td>` + v.username + `</td>
                                        <td>` + v.phone + `</td>
                                        <td>` + v.name + `</td>
                                        <td><input type="checkbox" value="` + v.id + `"  title="是|否" lay-filter="setadmin" lay-skin="switch" > </td>
                                 </tr>`;
                            });
                        } else {
                            users += ` <tr>
                                        <td  colspan="4">无数据</td>
                                      </tr>`;
                        }

                        laypage.render({
                            elem: 'pagebar',
                            limit: 20,
                            count: data.data.total // 数据总数
                        });
                        $('#users').html(users);
                        form.render();



                    } else {
                        toastr_error(data.message);
                    }


                },
                error: function() {

                },

            });
        }

        function selAdmin() {

            $.ajax({
                url: '/admin/cnf/Site/systemAdmin?type=seladmin&siteid=<?php echo request()->get('siteid') ?>', //请求路径
                type: "get", //请求方式
                data: {}, // 默认写法
                success: function(data) {
                    var seladmin = '';
                    if (data.code == 0) {
                        if (data.data.length > 0) {
                            $.each(data.data, function(i, v) {
                                seladmin += ` <tr>
                                        <td>` + v.username + `</td>
                                        <td>` + v.phone + `</td>
                                        <td>` + v.name + `</td>
                                        <td>
                                            <div class="rc-opbar" style="margin-left:15px">
                                                <a class="error delete" uid="` + v.id + `" href="javascript:;"> <i class="iconfont">&#xe643;</i> 删除</a>
                                            </div>
                                        </td>
                                 </tr>`;
                            });

                        } else {
                            seladmin += ` <tr>
                                        <td  colspan="4">无数据</td>
                                      </tr>`;
                        }

                        $('#admins').html(seladmin);

                    }

                },
                error: function() {

                },

            });

        }

        form.on('switch(setadmin)', function(data) {
            var elem = data.elem; // 获得 checkbox 原始 DOM 对象
            var value = elem.value; // 获得 checkbox 值
            if (elem.checked) {
                $.ajax({
                    url: '/admin/cnf/Site/systemAdmin', //请求路径
                    type: "POST", //请求方式
                    data: {
                        siteid: <?php echo request()->get('siteid') ?>,
                        uid: value
                    }, // 默认写法
                    success: function(data) {
                        if (data.code == 0) {
                            toastr_success("设置成功", function() {});
                        } else {
                            toastr_error(data.message);
                        }
                    },
                    error: function() {

                    },
                });

            }
        });


        //删除管理员
        $('body').on('click', '.delete', function() {

            var uid = $(this).attr('uid');
            layer.confirm('确定要删除么？', {
                icon: 3
            }, function() {
                layer.closeAll();
                $.ajax({
                    url: '/admin/cnf/Site/delSystemAdmin?siteid=<?php echo request()->get('siteid') ?>&uid=' + uid, //请求路径
                    type: "POST", //请求方式
                    success: function(data) {
                        if (data.code == 0) {
                            window.location.reload();
                        } else {
                            toastr_error(data.message);
                        }
                    },
                    error: function() {

                    },

                });


            }, function() {


            });

        })

    });
</script>